import router from "../router";
import { Cookies, showMassage } from "./common";
import requestApi from "../api/requestApi";
import { userStore } from "../store/user";
import { Axios, AxiosResponse } from "axios";
import { start, close } from "./nprogress";
let once = false;
router.beforeEach(async (to, from, next) => {
  const token = Cookies.getToken();
  // 进行特殊处理
  if (!token && to.path != "/login") {
    if (once) {
      showMassage("清先登录", "error");
    }
    once = true;
    return next("/login");
  } else if (token && to.path == "/login") {
    showMassage("请勿重复登录", "error");
    // 避免token
    return next({ path: from.path ? from.path : "/" });
  } else {
    next();
    start()
  }
  const user = userStore();
  if (token) {
    const res = await requestApi.getMenuList() as AxiosResponse;
    user.userinfo = res.data.data;
  }
});
router.afterEach(() => {
  close();
});